package cn.tips.dmp.biz.talent.wxApplet.service.impl;

import cn.tips.dmp.biz.talent.wxApplet.dto.WxCheckUserDTO;
import cn.tips.dmp.biz.talent.wxApplet.entity.WxCheckUserPO;
import cn.tips.dmp.biz.talent.wxApplet.repository.WxCheckUserRepository;
import cn.tips.dmp.biz.talent.wxApplet.service.WxCheckUserService;
import cn.tips.dmp.supports.jpa.PageParamsDTO;
import cn.tips.dmp.supports.jpa.SortParamsDTO;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor
public class WxCheckUserServiceImpl implements WxCheckUserService {

    private final WxCheckUserRepository wxCheckUserRepository;

    @Override
    public void saveWxCheckUserData(String name, String phoneNumber, Long orgId, String status) {
        //查询是否存在待审核
        WxCheckUserPO wxCheckUserPO = wxCheckUserRepository.findByNameAndPhoneAndOrgIdAndStatus(name, phoneNumber, orgId, status);
        if(wxCheckUserPO == null){
            wxCheckUserPO = new WxCheckUserPO();
            wxCheckUserPO.setName(name);
            wxCheckUserPO.setPhone(phoneNumber);
            wxCheckUserPO.setOrgId(orgId);
            wxCheckUserPO.setStatus(status);
            wxCheckUserRepository.save(wxCheckUserPO);
        }
    }

    @Override
    public Page<WxCheckUserPO> readAllByPage(WxCheckUserDTO searchDTO, PageParamsDTO pageParamsDto, SortParamsDTO sortParamsDto) {

//        QTipsWechatCheckUserPO qTipsWechatCheckUserPO = QTipsWechatCheckUserPO.TipsWechatCheckUserPO;
//
//        // 开启谓语查询条件
//        Predicate predicate = qTipsWechatCheckUserPO.id.isNotNull();
//
//        String name = searchDTO.getRealName();
//        if (!StringUtils.isEmpty(name)) {
//            predicate = ExpressionUtils.and(predicate, qTipsWechatCheckUserPO.name.eq(name));
//        }
//
//        String phone = searchDTO.getPhoneNumber();
//        if (!StringUtils.isEmpty(phone)) {
//            predicate = ExpressionUtils.and(predicate, qTipsWechatCheckUserPO.phone.eq(phone));
//        }
//
//        String status = searchDTO.getStatus();
//        if (!StringUtils.isEmpty(status)) {
//            predicate = ExpressionUtils.and(predicate, qTipsWechatCheckUserPO.status.eq(status));
//        }
//
//        //如果是机构端查询
//        Long orgId = searchDTO.getOrgId();
//        if (null != orgId && orgId.longValue() > 0) {
//            ExpressionUtils.and(predicate, qTipsWechatCheckUserPO.orgId.eq(orgId));
//        }
//
//        Sort sort;
//        if (null == sortParamsDto) {
//            sort = Sort.by(Sort.Direction.DESC, "createTime");
//        } else {
//            // 排序
//            sort = sortParamsDto.getSort();
//            if (null == sort) {
//                sort = Sort.by(Sort.Direction.DESC, "createTime");
//            }
//        }
//
//        // 执行返回
//        Pageable pageable = pageParamsDto.getPageableWithSort(sort);
//
//        return TIpsWechatCheckUserRepository.findAll(predicate, pageable);
        return null;
    }

    @Override
    public void updateCheckUserData(WxCheckUserDTO dto) {
        WxCheckUserPO wxCheckUserPO = wxCheckUserRepository.getOne(dto.getId());
        wxCheckUserPO.setStatus(dto.getStatus());
        wxCheckUserRepository.save(wxCheckUserPO);
    }
}
